<!-- 渠道信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="等级">
                            <a-select v-model="grade" placeholder="请选择" allowClear @change="getData">
                                <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button type="primary" style="margin-right: 20px" @click="getData()">查询</a-button>
                            <a-button type="" icon="plus" @click="add()">添加</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size, }" size="small">
            <div slot="grade" slot-scope="grade">
                <div v-for="(item,i) in gradeList" :key="item.id">
                    <a-tag v-if="item.grade == grade" :color="colors[i%13]">{{item.gradeName}}</a-tag>
                </div>
            </div>
            <div slot="needSonGrade" slot-scope="needSonGrade">
                <div v-for="(item,i) in gradeList" :key="item.id">
                    <a-tag v-if="item.grade == needSonGrade" :color="colors[i%13]">{{item.gradeName}}</a-tag>
                </div>
            </div>
            <div slot="isOn" slot-scope="isOn">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="isOn == 1 ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="update(record)">修改</a-button>
                <a-popconfirm
                    title="是否确认删除当前配置?"
                    ok-text="Yes"
                    cancel-text="No"
                    @confirm="del(record)"
                >
                    <a-button type="link">删除</a-button>
                </a-popconfirm>
            </div>
        </a-table>

        <a-modal
            title="添加、修改通道等级费率"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 14 }">
                    <a-form-item label="升级等级">
                        <a-select v-model="item.grade" placeholder="请选择升级等级" allowClear>
                            <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="本人商城消费金额" help="传0则不限制">
                        <a-input v-model="item.needBuyAmount"  placeholder="请输入本人商城消费金额" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="下级等级">
                        <a-select v-model="item.needSonGrade" placeholder="请选择下级等级" allowClear>
                            <a-select-option v-for="(item,i) in gradeList" :key="i" :value="item.grade">{{item.gradeName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="下级数量">
                        <a-input v-model="item.needSonGradeNumber" placeholder="请输入下级数量" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="状态">
                        <a-radio-group v-model="item.isOn" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryGradeUpConfig , updateGradeUpConfig } from '@/api/product';
import { queryGradeProduct } from '@/api/product';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            dataList: [],
            total: 0,
            isload: false,
            page: 1,
            size: 10,
            columns,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            confirmLoading: false,
            isAdd: true,
            visible: false,
            item: {
                'id': '',
                'grade': undefined, // 升级等级
                'channelTag': '', // 渠道标识
                'needBuyAmount': '', // 本人商城购买金额，传0则不限制
                'needSonGrade': undefined, // 需要下级的等级
                'needSonGradeNumber': '', // 需要下级的数量
                'isOn': 1, // 是否开启
            },
            grade: undefined,
            gradeList: [],
            statusList: [
                { id: 1, name: '开启' },
                { id: 0, name: '关闭' },
            ],
        };
    },
    async created(){
        try {
            this.gradeList = await this.getGrade(this.brandId)
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询等级信息
        getGrade(brandId){
            let _this = this
            return new Promise((resolve,reject)=>{
                const params = {
                    'brandId':brandId,
                    'size':99,
                    'page':0,
                }
                queryGradeProduct(params)
                .then(res=>{
                    if(res.success){
                        resolve(res.data.content)
                    }else{
                        reject()
                    }
                })
            })
        },
        // 查询渠道配置
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId': _this.brandId,
                'grade': _this.grade,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryGradeUpConfig(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.isAdd = true;
            this.item.id = '';
            this.item.grade = undefined;
            this.item.needBuyAmount = '';
            this.item.needSonGrade = undefined;
            this.item.needSonGradeNumber = '';
            this.item.isOn = 1;
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.item.rate = (item.rate * 100).toFixed(2);
            this.isAdd = false;
            this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(_this.item.grade == undefined){
                _this.$message.warning('请选择升级等级')
                return;
            }
            if(_this.item.needSonGrade == undefined){
                _this.$message.warning('请选择下级等级')
                return;
            }
            const params = {
                'id': _this.item.id,
                'brandId': _this.brandId,
                'needBuyAmount': _this.item.needBuyAmount == '' ? 0 : _this.item.needBuyAmount,
                'grade': _this.item.grade,
                'needSonGrade': _this.item.needSonGrade,
                'needSonGradeNumber': _this.item.needSonGradeNumber == '' ? 0 : _this.item.needSonGradeNumber,
                'isOn': _this.item.isOn,
            }
            updateGradeUpConfig(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false;
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'brandId': _this.brandId,
                'needBuyAmount': item.needBuyAmount,
                'grade': item.grade,
                'needSonGrade': item.needSonGrade,
                'needSonGradeNumber': item.needSonGradeNumber,
                'isOn': item.isOn,
                'isDelete': '1'
            }
            updateGradeUpConfig(params)
            .then(res=>{
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>
</style>